package com.hz.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.hz.beans.code.HzOperateCode;
import com.hz.beans.code.HzResultCode;
import com.hz.beans.user.entity.TConsuReply;
import com.hz.beans.user.entity.TCounselling;
import com.hz.sys.utils.ResponseUtils;
import com.hz.user.mapper.TConsuReplyMapper;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hz.base.service.impl.BaseService;
import com.hz.beans.user.entity.TReply;
import com.hz.user.mapper.TReplyMapper;
import com.hz.user.service.inf.ITReplyService;
import java.util.Date;
import java.util.List;

/**
 * TReplyService
 * @author mcz
 * @since [0.0.1]
 * @version [0.0.1,2018年10月19日]
 */
@Service("tReplyService")
public class TReplyService extends BaseService<TReply> implements ITReplyService{
	
	protected Logger logger = LoggerFactory.getLogger(TReplyService.class);
	
	@Autowired
	private TReplyMapper tReplyMapper;
	@Autowired
	private TConsuReplyMapper tConsuReplyMapper;

	/**
	 * @Author mcz
	 * @Description  添加回复的回复
	 * @Date 2018/10/23 11:04
	 * @Param 回复实体类，上级回复id
	 * @return
	 **/
	@Override
	public String addTReplyToReply(JSONObject object) {
		ResponseUtils response=new ResponseUtils();
		String result;
		response.setResponseOptDesc(HzOperateCode.ADD_TREPLY_CONSU.getOptDesc());
		int size=object.size();
		try {
			if(size!=3||!object.containsKey("replyPid")||!object.containsKey("content")||!object.containsKey("conId")){
				response.setResponseDesc("参数非法");
				result= response.failJSON(response);
			}else{
				Long replyPid=object.getLong("replyPid");
				String content=object.getString("content");
				Long conId=object.getLong("conId");
				if(StringUtils.isNotBlank(content)){
					TReply tReply = new TReply();
					TConsuReply tConsuReply = new TConsuReply();
					tReply.setCreateTime(new Date());
					//tReply.setWeChatName();
					tReply.setContent(content);
					//插入回复
					Integer tReplyInsertStatu = tReplyMapper.insert(tReply);
					//获取咨询的第一个回复id
					tReply.setConsuId(conId);
					TReply selectReply=tReplyMapper.selectByConsuId(tReply);
					tConsuReply.setParentId(selectReply.getId());
					tConsuReply.setChildId(tReply.getId());
					/*
					*  咨询
					*  回复1  第一个回复（根回复）
					*  回复2  针对1的回复
					*  回复3 针对2的回复
					* */
					//建立插入的回复与该咨询的第一个根回复的关系  3和1的关系
					Integer tConsuReplyInsertStatu = tConsuReplyMapper.insert(tConsuReply);
					//建立此回复与上次回复的子父关系 3和2的关系
					if(!selectReply.getId().equals(replyPid)){
						tConsuReply.setParentId(replyPid);
						tConsuReply.setChildId(tReply.getId());
						Integer tConsuReplyInsertStatu2=tConsuReplyMapper.insert(tConsuReply);
					}
					if(tReplyInsertStatu==1||tConsuReplyInsertStatu==1){
						response.setResponseDesc("插入成功");
						result=response.successJSON(response);
					}else{
						response.setResponseDesc("插入失败");
						result= response.failJSON(response);
					}
				}else{
					response.setResponseDesc("content参数为空");
					result= response.failJSON(response);
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			response.setResult(HzResultCode.SYSTEM_EXCEPTION.getResultDesc());
			result=response.failJSON(response);
            throw new RuntimeException(e);
		}
		return result;
	}

	@Override
	public String addTReplyConsu(TReply tReply, Long consuId) {
		tReply.setConsuId(consuId);
		ResponseUtils response=new ResponseUtils();
		String result;
		response.setResponseOptDesc(HzOperateCode.ADD_TREPLY_CONSU.getOptDesc());
		TConsuReply tConsuReply = new TConsuReply();
		tReply.setCreateTime(new Date());
		//tReply.setWeChatName();
		try {
			int insertRepFlg=tReplyMapper.insert(tReply);
			tConsuReply.setParentId(tReply.getId());
			tConsuReply.setChildId(tReply.getId());
			int consRepFlg=tConsuReplyMapper.insert(tConsuReply);
			if(insertRepFlg==1&&consRepFlg==1){
				response.setResponseDesc("添加成功");
				result=response.successJSON(response);
			}else{
				response.setResponseDesc("添加失败");
				result=response.failJSON(response);
			}
		} catch (Exception e) {
			logger.error("插入咨询回复异常{}",e.getMessage());
			response.setResponseDesc("添加异常");
			response.setResult(HzResultCode.SYSTEM_EXCEPTION.getResultDesc());
			result=response.failJSON(response);
		}
		return result;
	}

	@Override
	public List<TReply> queryTReplyAndQuesList(TCounselling tCounselling) {
		List<TReply> tReplyList=tReplyMapper.queryTReplyAndQuesList(tCounselling);
		return tReplyList;
	}






}
